package br.com.portalecbahia;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import br.com.portalecbahia.connectionservices.SynchronizeStadium;
import br.com.portalecbahia.customize.HorizontalPager;
import br.com.portalecbahia.dao.StadiumDAO;
import br.com.portalecbahia.listner.StadiumListeners;
import br.com.portalecbahia.model.Stadium;

import com.google.analytics.tracking.android.EasyTracker;
import com.raptureinvenice.webimageview.image.WebImageView;

public class StadiumActivity extends Activity implements StadiumListeners {
	

	private List<Stadium> listStadium;
	private Stadium stadiumDetail;
	private StadiumDAO dao;
	private HorizontalPager realViewSwitcher;
	private SynchronizeStadium taskStadium;
	private TextView titleTopTX;
	private TextView pageTX;
	private TextView maxTX;
	private ImageButton expandBTN;
	public int first = 0;
	private Button mapBTN;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.stadium_list);

		EasyTracker.getInstance().setContext(this);
	    realViewSwitcher  = (HorizontalPager) findViewById(R.id.listviewstadium);
		
		dao = new StadiumDAO(this);


		Bundle loadInfo = getIntent().getExtras();
		stadiumDetail = (Stadium) loadInfo.getSerializable("stadium");
		
		titleTopTX = (TextView)findViewById(R.id.titleTopTX);

		if(stadiumDetail!=null){
		
			titleTopTX.setText(stadiumDetail.getName());
		}
		Stadium stadiumCompleate = dao.findByPrimaryKey(stadiumDetail);
		if(stadiumCompleate!=null){
			stadiumDetail = stadiumCompleate;
		}
		
		pageTX= (TextView)findViewById(R.id.pageTX);
		maxTX = 		(TextView)findViewById(R.id.maxTX);

//        
//        TextView titleTopTX = (TextView)findViewById(R.id.titleTopTX);
//        titleTopTX.setText(stadiumDetail.getName());
        
		taskStadium = new SynchronizeStadium(this);
		taskStadium.execute();
		
		buildScreen(false);

		 

    }
    
    @Override
    public void onStart() {
      super.onStart();
      EasyTracker.getInstance().activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
      super.onStop();
      EasyTracker.getInstance().activityStop(this); // Add this method.
    }
	
	public void buildScreen(boolean first){
		
		listStadium =  dao.findAll();
		maxTX.setText(listStadium.size()+"");
		int page=0;
		for (int i = 0; i < listStadium.size(); i++) {
			LayoutInflater inflater = (LayoutInflater)
			this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = inflater.inflate(R.layout.item_list_horizontal_stadium, null);
			Stadium stadiumItem  = listStadium.get(i);
			if(stadiumItem.getUrlImage()!=null && !"".equals(stadiumItem.getUrlImage())){
				WebImageView myImage = (WebImageView)view.findViewById(R.id.stadiumIMG);
				WebImageView.setDiskCachingDefaultCacheTimeout(60 * 60* 24*30); // 1 month expiration
				myImage.setImageWithURL(this, stadiumItem.getUrlImage());
				expandBTN = (ImageButton)view.findViewById(R.id.expandBTN);
				expandBTN.setContentDescription(stadiumItem.getUrlImage());
			}
	            
			TextView capacityTX = (TextView)view.findViewById(R.id.capacityTX);
			capacityTX.setText(stadiumItem.getCapacity());
	            

			TextView builtTX = (TextView)view.findViewById(R.id.builtTX);
			builtTX.setText(stadiumItem.getDateInauguration());
	            
			TextView descriptionTX = (TextView)view.findViewById(R.id.descriptionTX);
			descriptionTX.setText(stadiumItem.getDescription());

			mapBTN = (Button)view.findViewById(R.id.mapBTN);
			mapBTN.setContentDescription(stadiumItem.getLatitude()+"/"+stadiumItem.getLongitude());

			
	        if(!first){    
			if(stadiumDetail!=null && stadiumDetail.getId()!=null){
				if(stadiumDetail.getId().equals(stadiumItem.getId())){
					page = i;
					titleTopTX.setText(stadiumItem.getName());
				}
			}
	        }else{
	        	if( i == 0){
					page = 0;
					titleTopTX.setText(stadiumItem.getName());
				
	        	}
	        }
	        realViewSwitcher.addView(view);
		}

			pageTX.setText(page+1+"");

		realViewSwitcher.setOnScreenSwitchListener(onScreenSwitchListener);
		
	    realViewSwitcher.setCurrentScreen(page, false);
	}
	
	public static Drawable LoadImageFromWebOperations(String url) {
	    try {
	        InputStream is = (InputStream) new URL(url).getContent();
	        Drawable d = Drawable.createFromStream(is, "src name");
	        return d;
	    } catch (Exception e) {
	        return null;
	    }
	}
	
	private final HorizontalPager.OnScreenSwitchListener onScreenSwitchListener =  new HorizontalPager.OnScreenSwitchListener() {
           @Override
           public void onScreenSwitched(final int screen) {
               // Check the appropriate button when the user swipes screens.
        		
   			int indice = screen;
   	        if(screen == listStadium.size()){
   	        	buildScreen(true);
   	        	return;
   	        }
   			Stadium stadium  = listStadium.get(indice);
   	                	
   			
   			
   	                    
   			titleTopTX = (TextView)findViewById(R.id.titleTopTX);
   			titleTopTX.setText(stadium.getName());
   	                    
   			
   			pageTX.setText(indice+1+"");
   			
           }

		
       };

	@Override
	public void onSynchronizeStadiumComplete(Boolean response) {
		buildScreen(false);
		
	}
	  
	public void openMap(View view){
		
		Intent intent = null;

		intent = new Intent(this, StadiumMap.class);
		intent.putExtra("coordinate", view.getContentDescription());

		startActivity(intent);
	}
	            
	public void openImage(View view){
		
		//set up dialog
        Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.expand_image);
        dialog.setCancelable(true);

        //set up image view
        WebImageView       webView= (WebImageView) dialog.findViewById(R.id.expandIMG);


        webView.setImageWithURL(this, view.getContentDescription()+"");

 
        dialog.show();
	}
	           
}
